package com.jn.mvp.presenter;

import android.content.Context;

import com.jn.mvp.contract.IdiomContract;
import com.jn.mvp.entity.Idiom;
import com.jn.mvp.http.GlobalRetrofitClient;
import com.jn.mvp.service.IdiomService;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class IdiomPresenter implements IdiomContract.IIdiomPresenter {

    private IdiomContract.IIdiomView mIdiomView;
    private Context mContext;

    public IdiomPresenter(IdiomContract.IIdiomView idiomView, Context mContext) {
        this.mIdiomView = idiomView;
        this.mContext = mContext;
    }

    @Override
    public void getData() {
        IdiomService service = GlobalRetrofitClient.getInstance().create(IdiomService.class);
        service.getData()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Idiom>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Idiom idiom) {
                        mIdiomView.showData(idiom);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mIdiomView.onHttpFail(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
